package com.jiangxun.leetcode.editor.cn;

//leetcode submit region begin(Prohibit modification and deletion)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    };

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

class Solution24 {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode cur = dummyHead;
        ListNode firstNode;
        ListNode secondNode;
        ListNode tempNode;
        while (cur.next != null && cur.next.next != null) {
            tempNode = cur.next.next.next;
            firstNode = cur.next;
            secondNode = cur.next.next;
            // 1
            cur.next = secondNode;
            // 2
            secondNode.next = firstNode;
            // 3
            firstNode.next = tempNode;
            cur = firstNode;
        }
        return dummyHead.next;
    }
}
// leetcode submit region end(Prohibit modification and deletion)
